﻿using System;
using System.Web.Security;
using SouthernLand.Common;
using SouthernLand.Common.BOL.User;

namespace SouthernLand.WebMaster.Membership
{
    public partial class CreateUser : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string[] roles = Roles.GetAllRoles();
                dlRoles.DataSource = roles;
                dlRoles.DataBind();
            }
            Set_ErrorMessage();
            if (Request.QueryString["Action"] == "Edit")
            {
                PanelLogon.Visible = false;
                btSave.Text = ResourceManager.GetString("Update");
                if (!IsPostBack)
                {
                    LoadProfile(Globals.GetStringFromQueryString("UserName"));
                }
            }
            else
            {
                txtLastName.Attributes.Add("onclick", "this.value=''");
                txtFirstName.Attributes.Add("onclick", "this.value=''");
            }
        }

        private void Set_ErrorMessage()
        {
            btSave.Text = ResourceManager.GetString("Register_Send");
            RegEmail.ErrorMessage = ResourceManager.GetString("Register_Email_error");
            ReqPassword.ErrorMessage = ResourceManager.GetString("Register_Password_error");
            CompareValRePassword.ErrorMessage = ResourceManager.GetString("Register_ConfirmPassword_error");
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["Action"] == "Edit")
            {
                try
                {
                    MembershipUser u =
                        System.Web.Security.Membership.GetUser(Globals.GetStringFromQueryString("UserName"));
                    u.Email = txtEmail.Text;
                    System.Web.Security.Membership.UpdateUser(u);
                    SaveProfile(Globals.GetStringFromQueryString("UserName"));
                    lblMsg.Text = ResourceManager.GetString("ModifiedSuccessfully");
                    //PanelMsg.Visible = true;
                }
                catch
                {
                    lblMsg.Text = ResourceManager.GetString("Register_DuplicateEmail");
                }
            }
            else
            {
                Page.Validate();
                if (Page.IsValid)
                {
                    // First get all the values
                    string user = txtUserName.Text;
                    string pswd = txtPassword.Text;
                    string email = txtEmail.Text;

                    string role = "";
                    if (dlRoles.SelectedItem.Value.ToLower() == Globals.Settings.AppRoles.DoanhNghiep.ToLower())
                    {
                        role = Globals.Settings.AppRoles.DoanhNghiep;
                    }
                    else if (dlRoles.SelectedItem.Value.ToLower() == Globals.Settings.AppRoles.KhachHang.ToLower())
                    {
                        role = Globals.Settings.AppRoles.KhachHang;
                    }
                    else
                        role = Globals.Settings.AppRoles.Admin;

                    MembershipCreateStatus status;
                    MembershipUser userObj = System.Web.Security.Membership.CreateUser(user, pswd, email, "yes", "yes",
                                                                                       true, out status);
                    if (userObj != null)
                    {
                        // Now add the user to the role and display message
                        Roles.AddUserToRole(userObj.UserName, role);
                        //Membership.UpdateUser
                        SaveProfile(user);
                        lblMsg.Text = ResourceManager.GetString("Register_CreatedSuccessfully");
                        PanelMsg.Visible = true;
                        //PanelUser.Visible = false;
                        Response.Redirect(Globals.ApplicationPath + "WebMaster/Membership/ManageUsers.aspx");
                    }
                    else
                    {
                        // These are just three values to check for in the status
                        // enumeration; there are others.
                        switch (status)
                        {
                            case MembershipCreateStatus.DuplicateEmail:
                                lblMsg.Text = ResourceManager.GetString("Register_DuplicateEmail");
                                break;
                            case MembershipCreateStatus.DuplicateUserName:
                                lblMsg.Text = ResourceManager.GetString("Register_DuplicateUserName");
                                break;
                            case MembershipCreateStatus.ProviderError:
                                lblMsg.Text = ResourceManager.GetString("Register_ProviderError");
                                break;
                            case MembershipCreateStatus.InvalidPassword:
                                lblMsg.Text = ResourceManager.GetString("Register_InvalidPassword");
                                break;
                            default:
                                lblMsg.Text = status + ".";
                                break;
                        }
                    }

                    PanelMsg.Visible = true;
                }
            }
        }

        protected void LoadProfile(string UserName)
        {
            string UserID = Common.BOL.User.User.GetUserIDByUserName(UserName);
            User obj = Common.BOL.User.User.GetUserByID(UserID);

            txtEmail.Text = obj.Email;
            txtAddess.Text = obj.Address;
            txtPhone.Text = obj.Phone;
            txtMobile.Text = obj.Mobile;
            txtFax.Text = obj.Fax;
            txtWebsite.Text = obj.Website;

            //jobSeeker
            txtFirstName.Text = obj.FirstName;
            txtLastName.Text = obj.LastName;

            //Employer
            txtCompanyName.Text = obj.Company;
            txtCompanyDesc.Text = obj.CompanyDescription;
        }

        public void SaveProfile(string UserName)
        {
            var obj = new User();
            obj.UserID = Common.BOL.User.User.GetUserIDByUserName(UserName);
            obj.UserName = UserName;
            obj.CountryID = 1;
            obj.ProvinceID = 1;
            obj.Email = txtEmail.Text;
            obj.Address = txtAddess.Text;
            obj.Phone = txtPhone.Text;
            obj.Mobile = txtMobile.Text;
            obj.Fax = txtFax.Text;


            //Employers
            obj.Company = txtCompanyName.Text;
            obj.CompanyDescription = txtCompanyDesc.Text;
            obj.Website = txtWebsite.Text;

            //JobSeeker
            obj.FirstName = txtFirstName.Text;
            obj.LastName = txtLastName.Text;
            obj.Gender = "Nam";
            obj.AddedBy = Globals.CurrentUserName;
            obj.AddedDate = DateTime.Now;
            obj.IsActive = true;

            if (Request.QueryString["Action"] == "Edit")
            {
                Common.BOL.User.User.Update(obj);
                Response.Redirect(Globals.ApplicationPath + "WebMaster/Membership/ManageUsers.aspx");
            }
            else
                Common.BOL.User.User.Insert(obj);
        }
    }
}